//
//  NIMileView.m
//  gweAD
//
//  Created by Navinfo on 2018/3/2.
//  Copyright © 2018年 mengy. All rights reserved.
//

#import "NIMileView.h"
#import <math.h>
#import "ColorDefine.h"

#define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180)

@interface NIMileView ()<CAAnimationDelegate>
@end

@implementation NIMileView{
    CAShapeLayer *progressLayer;
    CAShapeLayer *lineBgLayer;
    CAShapeLayer *lineLayer;
    CALayer *gradientLayer;
    UILabel *valueLabel;
    
    //动画
    BOOL stopAnimation;
    NSNumber *fromValue;
    NSNumber *toValue;
}


- (instancetype)initWithFrame:(CGRect)frame progress:(CGFloat)progress {
    if (self) {
        self = [super initWithFrame:frame];
        self.backgroundColor = [UIColor clearColor];
        [self setUI];
        self.progress = progress;
        
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - self.bounds.size.height/2, self.bounds.size.width, self.bounds.size.height/2)];
        [valueLabel setBackgroundColor:[UIColor clearColor]];
        [valueLabel setFont:[UIFont systemFontOfSize:20.0f]];
        [valueLabel setTextColor:[UIColor redColor]];
        [valueLabel setText:@"   计算中..."];
        [valueLabel setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:valueLabel];
        valueLabel.hidden = YES;
        
        lineBgLayer = [CAShapeLayer layer];
        [lineBgLayer setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        [self.layer addSublayer:lineBgLayer];
    }
    return self;
}

#pragma mark - 初始化页面
- (void)setUI {
    double radius = (self.bounds.size.width/2 - 2.0f);
    
    //外围虚线
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.lineCap = kCALineCapButt;
    bgLayer.lineWidth = 1.0f;
    bgLayer.strokeColor = [UIColor redColor].CGColor;
    bgLayer.fillColor = [UIColor clearColor].CGColor;
    bgLayer.strokeStart = 0.0f;
    bgLayer.strokeEnd = 1.0f;
    bgLayer.lineDashPattern = @[@2,@2];
    UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:radius startAngle:DEGREES_TO_RADIANS(150) endAngle:DEGREES_TO_RADIANS(30) clockwise:YES];
    bgLayer.path = bgPath.CGPath;
    
    [self.layer addSublayer:bgLayer];
    
    //进度外框
    double lineWidth = self.bounds.size.width/6.0f;
    radius = radius - self.bounds.size.width/6.0f/2 - 8;
    CAShapeLayer *bgLayer4 = [CAShapeLayer layer];
    bgLayer4.lineCap = kCALineCapButt;
    bgLayer4.lineWidth = lineWidth;
    bgLayer4.fillColor = [UIColor clearColor].CGColor;
    bgLayer4.strokeColor = [UIColor redColor].CGColor;
    bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:radius startAngle:DEGREES_TO_RADIANS(149) endAngle:DEGREES_TO_RADIANS(31) clockwise:YES];
    bgLayer4.path = bgPath.CGPath;
    [self.layer addSublayer:bgLayer4];
    
    //进度内框
    lineWidth = lineWidth - 2;
    bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:radius startAngle:DEGREES_TO_RADIANS(150) endAngle:DEGREES_TO_RADIANS(30) clockwise:YES];
    CAShapeLayer *bgLayer5 = [CAShapeLayer layer];
    bgLayer5.lineCap = kCALineCapButt;
    bgLayer5.lineWidth = lineWidth;
    bgLayer5.fillColor = [UIColor clearColor].CGColor;
    bgLayer5.strokeColor = [UIColor blackColor].CGColor;
    bgLayer5.path = bgPath.CGPath;
    [self.layer addSublayer:bgLayer5];
    
    //指针
    lineLayer = [CAShapeLayer layer];
    lineLayer.lineCap = kCALineCapButt;
    lineLayer.lineWidth = 1.0f;
    lineLayer.strokeColor = [UIColor redColor].CGColor;
    lineLayer.fillColor = nil;
    
    //圆心
    bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:4 + 3 + 5 startAngle:DEGREES_TO_RADIANS(0) endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
    
    CAShapeLayer *bgLayer2 = [CAShapeLayer layer];
    bgLayer2.lineCap = kCALineCapButt;
    bgLayer2.fillColor = [UIColor redColor].CGColor;
    bgLayer2.strokeStart = 0.0f;
    bgLayer2.strokeEnd = 1.0f;
    bgLayer2.path = bgPath.CGPath;
    [self.layer addSublayer:bgLayer2];
    [bgLayer2 setZPosition:3.0f];
    
    bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:4 startAngle:DEGREES_TO_RADIANS(0) endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
    
    CAShapeLayer *bgLayer3 = [CAShapeLayer layer];
    bgLayer3.lineCap = kCALineCapButt;
    bgLayer3.lineWidth = 3.0f;
    bgLayer3.strokeColor = [UIColor redColor].CGColor;
    bgLayer3.fillColor = [UIColor blackColor].CGColor;
    bgLayer3.strokeStart = 0.0f;
    bgLayer3.strokeEnd = 1.0f;
    
    bgLayer3.path = bgPath.CGPath;
    [self.layer addSublayer:bgLayer3];
    [bgLayer3 setZPosition:4.0f];
    
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)];
    [linePath addLineToPoint:CGPointMake((self.bounds.size.width/2 - radius - lineWidth/2), self.bounds.size.height/2)];
    lineLayer.path = linePath.CGPath;
    
    [self.layer addSublayer:lineLayer];
    
    progressLayer = [CAShapeLayer layer];
    progressLayer.lineWidth = lineWidth;
    progressLayer.lineCap = kCALineCapButt;
    progressLayer.fillColor = [UIColor clearColor].CGColor;
    progressLayer.strokeColor = [UIColor redColor].CGColor;
    progressLayer.path = bgPath.CGPath;
    [self.layer addSublayer:progressLayer];
    
    
    [self gradient];
}

//渐变层
- (void)gradient {
    gradientLayer = [CALayer layer];
    
    CAGradientLayer *leftGradient = [CAGradientLayer layer];
    leftGradient.frame = CGRectMake(0, 0, self.bounds.size.width / 2 , self.bounds.size.height);
    leftGradient.colors = @[(id)[UIColor redColor].CGColor,
                            (id)[UIColor redColor].CGColor];
    [leftGradient setLocations:@[@0.3, @0.9, @1]];
    CAGradientLayer *rightGradient = [CAGradientLayer layer];
    rightGradient.frame = CGRectMake(self.bounds.size.width / 2, 0, self.bounds.size.width / 2 , self.bounds.size.height);
    rightGradient.colors = @[(id)[UIColor redColor].CGColor,
                             (id)[UIColor redColor].CGColor];
    [rightGradient setLocations:@[@0.3, @0.9, @1]];
    [gradientLayer addSublayer:leftGradient];
    [gradientLayer addSublayer:rightGradient];
    [gradientLayer setMask:progressLayer];
    [self.layer addSublayer:gradientLayer];
}


#pragma mark - setMethod
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    [lineLayer removeFromSuperlayer];
    [progressLayer removeFromSuperlayer];
    
    UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.width/2) radius:(self.bounds.size.width/2 - 2 - 8 - self.bounds.size.width/6.0f/2.0f) startAngle:DEGREES_TO_RADIANS(150) endAngle:DEGREES_TO_RADIANS(30) clockwise:YES];
    progressLayer = [CAShapeLayer layer];
    progressLayer.lineWidth = self.bounds.size.width/6.0f;
    progressLayer.lineCap = kCALineCapButt;
    progressLayer.fillColor = [UIColor clearColor].CGColor;
    progressLayer.strokeColor = [UIColor redColor].CGColor;
    progressLayer.path = bgPath.CGPath;
    progressLayer.strokeEnd = _progress;
    [progressLayer setZPosition:2.0f];
    [gradientLayer setMask:progressLayer];
    
    
    double degrees = (_progress*(360 - 120) - 30)*M_PI/180.0;
    UIBezierPath *linePath = [self getLinePath:degrees];
    lineLayer = [CAShapeLayer layer];
    lineLayer.lineCap = kCALineCapButt;
    lineLayer.lineWidth = 1.0f;
    lineLayer.strokeColor = [UIColor redColor].CGColor;
    lineLayer.fillColor = nil;
    lineLayer.path = linePath.CGPath;
    [lineBgLayer addSublayer:lineLayer];
    
}

- (UIBezierPath *)getLinePath:(double)degrees{
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)];
    CGFloat x = 10 + ((self.bounds.size.width/2) - 2 - 8) - ((self.bounds.size.width/2) - 2 - 8)*cos(degrees);
    CGFloat y = 10 + ((self.bounds.size.width/2) - 2 - 8) - ((self.bounds.size.width/2) - 2 - 8)*sin(degrees);
    [linePath addLineToPoint:CGPointMake(x,y)];
    return linePath;
}

- (void)startAnimaition
{
    if (!fromValue) {
        fromValue = @0.0f;
        toValue = @1.0f;
    }
    [self setProgress:0.0f];
    stopAnimation = NO;
    [self animaition];
    valueLabel.hidden = NO;
}

- (void)stopAnimaition{
    stopAnimation = YES;
    [progressLayer removeAllAnimations];
    [lineBgLayer removeAllAnimations];
}

- (void)animaition{
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    ani.fromValue = fromValue;
    ani.toValue = toValue;
    ani.duration = 1.5f;
    ani.fillMode=kCAFillModeForwards;
    ani.removedOnCompletion=NO;
    ani.repeatCount = 1;
    ani.delegate = self;
    [progressLayer addAnimation:ani forKey:nil];
    
    CABasicAnimation * lineani = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    if (fromValue.floatValue > 0) {
        lineani.toValue = [NSNumber numberWithFloat:0.0];
        lineani.fromValue = [NSNumber numberWithFloat:(8.0f/6.0f)*M_PI];
    }else{
        lineani.fromValue = [NSNumber numberWithFloat:0.0];
        lineani.toValue = [NSNumber numberWithFloat:(8.0f/6.0f)*M_PI];
    }
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    lineani.cumulative = NO;
    lineani.beginTime = 0;
    lineani.duration = 1.5f;
    lineani.fillMode = kCAFillModeForwards;
    lineani.removedOnCompletion = NO;
    [lineBgLayer addAnimation:lineani forKey:nil];
    
}



- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if (!stopAnimation) {
        if (fromValue.floatValue > 0) {
            fromValue = @0.0f;
            toValue = @1.0f;
        }else{
            fromValue = @1.0f;
            toValue = @0.0f;
        }
        [self animaition];
    }else{
        valueLabel.hidden = YES;
        self.progress = _progress;
    }
}
@end
